/////////////////////////////////////////////////////////////////////////////
// Name:        Property_Diag.cpp
// Purpose:     list of properties
// Author:      Matthew Gong
// Created:     07/05/2005
// Copyright:   (c) Matthew Gong
// Licence:     GPL licence
/////////////////////////////////////////////////////////////////////////////

#ifdef __GNUG__
// #pragma implementation
#endif

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

// -*- C++ -*- generated by wxGlade 0.4cvs on Tue Jul  5 10:39:29 2005

#include "Property_Diag.h"

#define GRIDNUM 300

PropertyDialog::PropertyDialog(wxWindow* parent, int id, const wxString& title, const wxPoint& pos, const wxSize& size, long style):
    wxDialog(parent, id, title, pos, size, style)
{
    // begin wxGlade: PropertyDialog::PropertyDialog
    label_sel =  new wxStaticText(this, -1, _("Current :"));
    text_ctrl_sel = new wxTextCtrl(this, -1, wxT(""));
    label_info =  new wxStaticText(this, -1, _("info"));
    grid_prop = new wxGrid(this, ID_PROPERTY);
    button_filter = new wxButton(this, ID_FILTER, _("Filter"));
    text_ctrl_filter = new wxTextCtrl(this, ID_FILTER, wxT(""), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
    button_showall = new wxButton(this, ID_SHOWALL, _("Show All"));
    button_ok = new wxButton(this, wxID_OK, _("OK"));
    button_cancel = new wxButton(this, wxID_CANCEL, _("Cancel"));

    set_properties();
    do_layout();
    // end wxGlade
}


BEGIN_EVENT_TABLE(PropertyDialog, wxDialog)
    // begin wxGlade: PropertyDialog::event_table
    EVT_BUTTON(ID_FILTER, PropertyDialog::OnFilter)
    EVT_GRID_CELL_LEFT_CLICK(PropertyDialog::OnClickGrid) 
    EVT_TEXT_ENTER(ID_FILTER, PropertyDialog::OnFilter)
    EVT_BUTTON(ID_SHOWALL, PropertyDialog::OnShowAll)
    EVT_BUTTON(wxID_OK, PropertyDialog::OnOK)
    // end wxGlade
END_EVENT_TABLE();


void PropertyDialog::OnFilter(wxCommandEvent &event)
{
    wxString tmp = GetResult();
    Update(text_ctrl_filter->GetValue());
    grid_prop->SelectRow(0);
    Select(tmp);
}


// wxGlade: add PropertyDialog event handlers

void PropertyDialog::OnShowAll(wxCommandEvent &event)
{
    wxString tmp = GetResult();
    Update();
    grid_prop->SelectRow(0);
    Select(tmp);
}

void PropertyDialog::OnClickGrid(wxGridEvent &event)
{
    text_ctrl_sel->SetValue(grid_prop->GetCellValue(event.GetRow(), 0));
    grid_prop->SelectRow(event.GetRow());
}

void PropertyDialog::OnOK(wxCommandEvent &event)
{
    event.Skip();
}


void PropertyDialog::set_properties()
{
    // begin wxGlade: PropertyDialog::set_properties
    text_ctrl_sel->SetMinSize(wxSize(300, -1));
    grid_prop->CreateGrid(GRIDNUM, 5);
    grid_prop->SetSelectionMode(wxGrid::wxGridSelectRows);
    grid_prop->SetColLabelValue(0, _("property name"));
    grid_prop->SetColSize(0, 300);
    grid_prop->SetColLabelValue(1, _("description"));
    grid_prop->SetColSize(1, 350);
    grid_prop->SetColLabelValue(2, _("unit"));
    grid_prop->SetColSize(2, 100);
    grid_prop->SetColLabelValue(3, _("access"));
    grid_prop->SetColSize(3, 100);
    grid_prop->SetColLabelValue(4, _("comment"));
    grid_prop->SetColSize(4, 800);
    grid_prop->SetMinSize(wxSize(800, 450));
    grid_prop->EnableEditing(false);
    grid_prop->EnableDragGridSize(false);
    grid_prop->EnableDragRowSize(false);
    button_cancel->SetDefault();
    // end wxGlade

    properties.reserve(256);
}


void PropertyDialog::do_layout()
{
    // begin wxGlade: PropertyDialog::do_layout
    wxBoxSizer* sizer_1 = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* sizer_2 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_3 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_4 = new wxBoxSizer(wxHORIZONTAL);
    sizer_4->Add(label_sel, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER_VERTICAL, 5);
    sizer_4->Add(text_ctrl_sel, 1, wxLEFT|wxRIGHT|wxALIGN_CENTER_VERTICAL, 5);
    sizer_4->Add(label_info, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER_VERTICAL, 5);
    sizer_1->Add(sizer_4, 0, wxALL, 5);
    sizer_1->Add(grid_prop, 1, wxALL|wxEXPAND, 10);
    sizer_3->Add(button_filter, 0, wxLEFT|wxALIGN_CENTER_VERTICAL, 5);
    sizer_3->Add(text_ctrl_filter, 1, wxLEFT|wxRIGHT|wxALIGN_CENTER_VERTICAL, 5);
    sizer_3->Add(button_showall, 0, wxRIGHT|wxALIGN_CENTER_VERTICAL, 5);
    sizer_1->Add(sizer_3, 0, wxTOP|wxBOTTOM|wxEXPAND, 5);
    sizer_2->Add(button_ok, 1, wxLEFT|wxRIGHT, 20);
    sizer_2->Add(button_cancel, 1, wxLEFT|wxRIGHT, 20);
    sizer_1->Add(sizer_2, 0, wxTOP|wxBOTTOM|wxEXPAND, 10);
    SetAutoLayout(true);
    SetSizer(sizer_1);
    sizer_1->Fit(this);
    sizer_1->SetSizeHints(this);
    Layout();
    // end wxGlade
}


void PropertyDialog::Load(const wxArrayString & name, const wxArrayString & desc, const wxArrayString & unit, const wxArrayString & access, const wxArrayString & comment)
{
  for (int i=0; i < name.GetCount(); ++i)
  {
    PropertyItem tmp;
    tmp.name = name[i];
    tmp.desc = desc[i];
    tmp.unit = unit[i];
    tmp.access = access[i];
    tmp.comment = comment[i];
    properties.push_back(tmp);
  }
  size = properties.size();
  Update();
}

void PropertyDialog::AddItem(const wxString & name, const wxString & desc, const wxString & unit, const wxString & access, const wxString & comment)
{
  PropertyItem tmp;
  tmp.name = name;
  tmp.desc = desc;
  tmp.unit = unit;
  tmp.access = access;
  tmp.comment = comment;
  properties.push_back(tmp);
}

void PropertyDialog::ClearVar()
{
  while (properties.size() > size)
    properties.pop_back();
}

wxString PropertyDialog::GetResult() const
{
  return text_ctrl_sel->GetValue();
}

void PropertyDialog::Update(const wxString & filter)
{
  grid_prop->BeginBatch();
  grid_prop->ClearGrid();
  std::vector<PropertyItem>::iterator i;
  int j;
  for (i = properties.begin(), j = 0; i != properties.end() && j < GRIDNUM; ++i)
  {
    if (pass_filter(*i, filter) )
    {
      grid_prop->SetCellValue(j, 0, (*i).name);
      grid_prop->SetCellValue(j, 1, (*i).desc);
      grid_prop->SetCellValue(j, 2, (*i).unit);
      grid_prop->SetCellValue(j, 3, (*i).access);
      grid_prop->SetCellValue(j, 4, (*i).comment);
      ++j;
    }
  }
  label_info->SetLabel(wxString::Format(_("show %d, hide %d"), j, properties.size()-j));
  grid_prop->EndBatch();
}

bool PropertyDialog::pass_filter(const PropertyItem & item, const wxString & filter)
{
  if (filter.IsEmpty())
    return true;
  if (item.name.Find(filter) != -1)
    return true;
  if (item.desc.Find(filter) != -1)
    return true;
  if (item.unit.Find(filter) != -1)
    return true;
  if (item.access.Find(filter) != -1)
    return true;
  if (item.comment.Find(filter) != -1)
    return true;
  return false;
}

bool PropertyDialog::Select(const wxString & name)
{
  text_ctrl_sel->SetValue(name);

  wxString tmp;
  int s=0;
  int xUnit, yUnit;
  int width, height;
  grid_prop->GetClientSize(&width, &height);
  grid_prop->GetScrollPixelsPerUnit(&xUnit, &yUnit);
  for (int j = 0; ! (tmp = grid_prop->GetCellValue(j, 0)).IsEmpty(); ++j)
  {
    if (tmp.BeforeFirst('[') == name.BeforeFirst('['))
    {
      grid_prop->SelectRow(j);
      grid_prop->Scroll(0, (s-height/2)/yUnit);
      return true;
    }
    s+= grid_prop->GetRowSize(j);
  }
  grid_prop->SelectRow(0);
  grid_prop->Scroll(0, 0);
  text_ctrl_sel->SetValue(grid_prop->GetCellValue(0, 0));
  return false;
}

